package com.luozongyao.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozongyao.admin.entity.Admin;
import com.luozongyao.admin.mapper.AdminMapper;
import com.luozongyao.admin.service.AdminService;
import com.luozongyao.commons.MD5;
import com.luozongyao.commons.R;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luozongyao
 * @since 2021-11-05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public R updateAdminPassWord(String oldPass, String pass, Integer adminId) {
        Admin admin = baseMapper.selectById(adminId);
        // 密码是加密过的
        // 比较旧密码是否和未更新的数据库密码是否一样，不一样则不让修改
        if (MD5.encrypt(oldPass+admin.getSalt()).equals(admin.getAdminPwd())) {
            // 设置新密码，加密
            String uuid = UUID.randomUUID().toString().replace("-","");
            admin.setSalt(uuid);
            admin.setAdminPwd(MD5.encrypt(pass+uuid));
            int result = baseMapper.updateById(admin);
            System.out.println(result);
            if (result == 1) {
                return R.ok();
            }
        }
        return R.error();
    }
}
